//
//  LoginViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 17.03.25.
//

import Foundation
import SwiftUI

class LoginViewModel : ObservableObject {
    
    @Published var isLoading = false
    @Published var loginState = ""
    
    func isWXAppInstalled() -> Bool {
        return WXApi.isWXAppInstalled()
    }
    
    func trialLogin() {
        KoiAccountingNetApi.standard.loginTrial { result in
            switch result {
            case .success(let user):
                KoiAccountingUserManager.standard.login(user: user)
                self.isLoading = false
                self.loginState = "success"
            case .failure(let failure):
                self.isLoading = false
                self.loginState = "fail"
                logger.error("wechat login fail \(failure)")
            }
        }
    }
    
    func weichatLogin() {
        self.isLoading = true
        WeChatAuthManager.shared.login { result in
            switch result {
            case .success(let success):
                print("微信登录成功 wechat login success \(success)")
                KoiAccountingNetApi.standard.loginThird(loginParams: success) { result in
                    switch result {
                    case .success(let user):
                        KoiAccountingUserManager.standard.login(user: user)
                        self.isLoading = false
                        self.loginState = "success"
                    case .failure(let error):
                        self.isLoading = false
                        self.loginState = "fail"
                        logger.error("wechat login fail \(error)")
                    }
                }
            case .failure(let error):
                self.isLoading = false
                self.loginState = "fail"
                logger.error("wechat login fail \(error)")
            }
        }
    }
}
